package com.xmcowell.sportpacemaster.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import java.util.ArrayList;
import java.util.List;

/**
 * 在这儿增加过�?��
 *
 * @author 604
 */
public class PreferencesUtils {

    // ctrl+shift+x 转为大写 ctrl+shift+y 转为小写
    private static SharedPreferences preferences;

    /**
     * cn.icnt.dinners.utils.Keys
     *
     * @author Andrew Lee <br/>
     *         create at 2014�?�?7�?下午7:39:32
     */
    public static class Keys {
        /**
         * 版本�?
         */
        public static final String VERSION = "version";

        public static final String TEL = "tel";
        public static final String PASSWORD = "password";
        public static final String OPEN_ID = "openid";
        public static final String LOGIN_TYPE = "login_type";//0未登录，1账号登录，2第三方登录
        public static final String IS_FIRST = "is_first";//0未登录，1账号登录，2第三方登录
        public static final String TOKEN = "token";
        /**
         * sharedPreference 文件�?
         */
        public static final String USERINFO = "userInfo";
        public static final String TOPIC = "topic";
        public static final String HAS_QUIT = "has_quit";
        public static final String LAT = "lat";//纬度
        public static final String LON = "lon";//经度
        public static final String FIRST_FLAG = "first_flag";//是否是第一次登录
        public static final String ACCOUNT = "account";//账号
        public static final String PWD = "password";//密码

        public static final String TOTAL_DISTANCE = "total_distance";//保存的距离，米
        public static final String TOTAL_TIME = "total_time";//保存的时长，秒
        public static final String Browser_History_Size = "browser_history_size";//保存的浏览器地址
        public static final String Browser_History_Value = "browser_history_value";//保存的浏览器地址
        public static final String Wifi_BSSID = "wifi_bssid";//保存已经连接成功的wifi_bssid;
        public static final String Wifi_PWD = "wifi_pwd";//保存已经连接成功的wifi密码

        public static final String PROCESS_NAME = "process_name";//记录开启的进程名称

    }

    /**
     * 存储布尔�?	 *
     *
     * @param mContext
     * @param key
     * @param value
     */
    public static void putBooleanToSPMap(Context mContext, String key,
                                         boolean value) {
        preferences = mContext.getSharedPreferences(Keys.USERINFO,
                Context.MODE_PRIVATE);
        Editor edit = preferences.edit();
        edit.putBoolean(key, value);
        edit.commit();
    }

    public static void putIntToSPMap(Context mContext, String key, int value) {
        preferences = mContext.getSharedPreferences(Keys.USERINFO,
                Context.MODE_PRIVATE);
        Editor edit = preferences.edit();
        edit.putInt(key, value);
        edit.commit();
    }

    public static int getIntFromSPMap(Context mContext, String key) {
        preferences = mContext.getSharedPreferences(Keys.USERINFO,
                Context.MODE_PRIVATE);
        int value = preferences.getInt(key, 0);
        return value;
    }

    /**
     * 获取布尔�?	 *
     *
     * @param mContext
     * @param key
     * @return
     */
    public static Boolean getBooleanFromSPMap(Context mContext, String key) {
        preferences = mContext.getSharedPreferences(Keys.USERINFO,
                Context.MODE_PRIVATE);
        boolean value = preferences.getBoolean(key, false);
        return value;
    }

    /**
     * 获取布尔�?	 *
     *
     * @param mContext
     * @param key
     * @return
     */
    public static Boolean getBooleanFromSPMap(Context mContext, String key, Boolean defaul) {
        preferences = mContext.getSharedPreferences(Keys.USERINFO,
                Context.MODE_PRIVATE);
        boolean value = preferences.getBoolean(key, defaul);
        return value;
    }

    /**
     * 保存list
     *
     * @param mContext
     * @param sizeKey
     * @param valueKey
     * @param list
     */
    public static void putListToSPMap(Context mContext, String sizeKey, String valueKey,
                                      List<String> list) {
        preferences = mContext.getSharedPreferences(Keys.USERINFO, mContext.MODE_PRIVATE);
        SharedPreferences.Editor mEdit1 = preferences.edit();
        mEdit1.putInt(sizeKey, list.size());

        for (int i = 0; i < list.size(); i++) {
            mEdit1.remove(valueKey + i);
            mEdit1.putString(valueKey + i, list.get(i));
        }
        mEdit1.commit();
    }

    /**
     * 获取list
     *
     * @param mContext
     * @param sizeKey
     * @param valueKey
     * @return list
     */
    public static List<String> getListFromSPMap(Context mContext, String sizeKey, String valueKey) {
        preferences = mContext.getSharedPreferences(Keys.USERINFO, mContext.MODE_PRIVATE);
        List<String> list = new ArrayList<>();
        int size = preferences.getInt(sizeKey, 0);
        for (int i = 0; i < size; i++) {
            list.add(preferences.getString(valueKey + i, null));
        }
        return list;
    }

    /**
     * 存储String
     *
     * @param mContext
     * @param key
     * @param value
     */
    public static void putValueToSPMap(Context mContext, String key,
                                       String value) {
        preferences = mContext.getSharedPreferences(Keys.USERINFO,
                Context.MODE_PRIVATE);
        Editor edit = preferences.edit();
        edit.putString(key, value);
        edit.commit();
    }

    /**
     * 获取String
     *
     * @param mContext
     * @param key
     * @return value
     */
    public static String getValueFromSPMap(Context mContext, String key) {
        if (null != mContext) {
            preferences = mContext.getSharedPreferences(Keys.USERINFO,
                    Context.MODE_PRIVATE);
            String value = preferences.getString(key, "");
            return value;
        } else {
            return null;
        }
    }

    /**
     * 获取String
     *
     * @param mContext
     * @param key
     * @param defaults 无�?时取defaults
     * @return
     */
    public static String getValueFromSPMap(Context mContext, String key,
                                           String defaults) {
        if (null != mContext) {
            preferences = mContext.getSharedPreferences(Keys.USERINFO,
                    Context.MODE_PRIVATE);
            String value = preferences.getString(key, defaults);
            return value;
        } else {
            return null;
        }
    }

    /**
     * 存储Float
     *
     * @param mContext
     * @param key
     * @param f
     */
    public static void putFloatFromSPMap(Context mContext, String key,
                                         Float f) {
        preferences = mContext.getSharedPreferences(Keys.USERINFO,
                Context.MODE_PRIVATE);
        Editor edit = preferences.edit();
        edit.putFloat(key, f);
        edit.commit();
    }

    /**
     * 获取Float
     *
     * @param mContext
     * @param key
     * @param defaults
     * @return
     */
    public static Float getFloatFromSPMap(Context mContext, String key,
                                          Float defaults) {
        if (null != mContext) {
            preferences = mContext.getSharedPreferences(Keys.USERINFO,
                    Context.MODE_PRIVATE);
            Float value = preferences.getFloat(key, defaults);
            return value;
        } else {
            return null;
        }
    }


    /**
     * 清除全部
     *
     * @param mContext
     */
    public static void clearSPMap(Context mContext) {
        preferences = mContext.getSharedPreferences(Keys.USERINFO,
                Context.MODE_PRIVATE);
        Editor edit = preferences.edit();
        edit.clear();
        edit.commit();
    }

    /**
     * 指定key清除
     *
     * @param mContext
     * @param key
     */
    public static void clearSpMap(Context mContext, String key) {
        putValueToSPMap(mContext, key, "");
    }

    /**
     * 用户注销 清除指定key
     *
     * @param mContext
     */
    public static void logout_del(Context mContext) {
        putValueToSPMap(mContext, Keys.TEL, null);
        putValueToSPMap(mContext, Keys.PASSWORD, null);
        // putValueToSPMap(mContext, Keys.NICKNAME, null);
        // putValueToSPMap(mContext, Keys.ORDERNO_NO, null);
        // putValueToSPMap(mContext, Keys.COLLECT_NO,null);
        // putValueToSPMap(mContext, Keys.COUPON_NO, null);
        // putValueToSPMap(mContext, Keys.CREDITS_NO, null);
        // putValueToSPMap(mContext, Keys.ACCOUNT_NO,null);
        // putValueToSPMap(mContext, Keys.TOKEN, null);
        // putValueToSPMap(mContext, Keys.USER_PORTRAIT,null);
    }
}
